package com.facebank.usersupport.service.impl;

import com.facebank.usersupport.mapper.usersupport.usersupport.ActivityMapper;
import com.facebank.usersupport.mapper.usersupport.usersupport.RouteMapper;
import com.facebank.usersupport.model.Activity;
import com.facebank.usersupport.model.RestModel;
import com.facebank.usersupport.model.Route;
import com.facebank.usersupport.service.IActivityService;
import com.facebank.usersupport.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActivityServiceImpl implements IActivityService{
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private RouteMapper routeMapper;

    @Override
    public RestModel add(Integer id) {
        Route route = routeMapper.selectByPrimaryKey(id);
        Activity activity = new Activity();
        activity.setCost(route.getCost());
        activity.setDetail(route.getDetail());
        activity.setImg(route.getImg());
        activity.setPrompt(route.getPrompt());
        activity.setRouteName("未编辑");
        activity.setStatus((short) 0);
        activity.setRouteType(route.getRouteType().shortValue());
        activity.setTitle(route.getTitle());
        activity.setCreateTime(System.currentTimeMillis());
        activity.setUpdateTime(System.currentTimeMillis());
        int is_success = activityMapper.insertSelective(activity);
        if (is_success==1){return new RestModel(RestModel.CODE_SUCCESS, RestModel.MESSAGE_SUCCESS);}
        return new RestModel();
    }

    @Override
    public RestModel list(Integer pageNo, Integer length, String routeName, Short routeType, Short status, Long startTime, Long endTime) {
        status = (short) StrUtil.parseStringToInt(status,-1);
        PageHelper.startPage(pageNo,length);
        List<Activity> activities = activityMapper.selectRouteList(routeName,routeType,status,startTime,endTime);
        PageInfo<Activity> pageInfo =new PageInfo<>(activities);
        return new RestModel(pageInfo);
    }

    @Override
    public RestModel delete(Integer id) {
        int is_success = activityMapper.deleteByPrimaryKey(id);
        if (is_success==1){return new RestModel(RestModel.CODE_SUCCESS, RestModel.MESSAGE_SUCCESS);}
        return new RestModel();
    }

    @Override
    public RestModel detail(Integer id) {
        return new RestModel(activityMapper.selectByPrimaryKey(id));
    }

    @Override
    public RestModel updata(Activity activity) {
        activity.setUpdateTime(System.currentTimeMillis());
        int is_success = activityMapper.updateByPrimaryKeySelective(activity);
        if (is_success==1){return new RestModel(RestModel.CODE_SUCCESS, RestModel.MESSAGE_SUCCESS);}
        return new RestModel();
    }

    @Override
    public RestModel introduce(Integer id) {
        Activity activity = new Activity();
        activity.setId(id);
        activity.setStatus((short)1);
        activity.setUpdateTime(System.currentTimeMillis());
        int is_success = activityMapper.updateByPrimaryKeySelective(activity);
        if (is_success==1){return new RestModel(RestModel.CODE_SUCCESS, RestModel.MESSAGE_SUCCESS);}
        return new RestModel();
    }
}
